import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
// 导入nprogress的css
import 'nprogress/nprogress.css'
import backend from '../views/layout/aside/../tar/BackIndex'
import index from '../views/web/WebClientsPage'
// import index from '../test/MyTest'
// import index from '../components/particles/ParticlesView'
// import VueCookies from 'vue-cookies'
// import request from '@/api/request'
Vue.use(VueRouter)

// 全局变量，用于存储用户角色信息
const routes = [
  {
    path: '/',
    name: 'index',
    component: index,
    redirect: 'webPage.html',
    children: [
      {
        path: '/webPage.html',
        name: 'webPage',
        component: () => import('../views/web/index/WebPage')
      },
      {
        path: '/dateShow',
        name: 'dateShow',
        component: () => import('../components/DateShow')
      },
      {
        path: '/details.html/:id.html',
        name: 'details',
        component: () => import('../views/classmedicine/DrugDetails')
        // props: true
      },
      {
        path: '/westernMedicine.html',
        name: 'westernMedicines',
        component: () => import('../views/web/index/WesternMedicine')
        // props: true
      },
      {
        path: '/westernMedicine',
        name: 'westernMedicine',
        component: () => import('../views/classmedicine/WesternDrugDetails')
        // props: true
      },
      {
        path: '/cagetoryMedicine/:category.html',
        name: 'cagetoryMedicine',
        component: () => import('../views/cagetory/classlable/ChaineTranditionalMedicine')
        // props: true
      },
      {
        path: '/westernCagetoryMedicine/:category.html',
        name: 'westernCagetoryMedicine',
        component: () => import('../views/cagetory/classlable/ChaineWesternMedicine')
        // props: true
      },
      {
        path: '/tranditionalMedicineCategory.html',
        name: 'tranditionalMedicineCategory',
        component: () => import('../views/cagetory/MoreMedicineCategory')
        // props: true
      },
      {
        path: '/backLogin.html',
        name: 'backLogin',
        component: () => import('../components/particles/ParticlesView')
      },
      {
        path: '/traditionalChineseMedicine',
        name: 'traditionalChineseMedicine',
        component: () => import('../views/medicinecagetory/TraditionalChineseMedicine')// 传统中医药
      },
      {
        path: '/chineseMedicines',
        name: 'chineseMedicines',
        component: () => import('../views/medicinecagetory/ProprietaryChineseMedicines')// 中成药
      },
      {
        path: '/westernMedicineTheme',
        name: 'westernMedicineTheme',
        component: () => import('../views/medicinecagetory/WesternMedicineTheme')// 西药
      },
      {
        path: '/apiMedicineInfo.html',
        name: 'apiMedicineInfo',
        component: () => import('../views/medicineapi/MedicineApiWarehouse')// 第三方药品信息
      },
      {
        path: '/healthCareProducts.html',
        name: 'healthCareProducts',
        component: () => import('../views/medicineapi/HealthCareProducts')// 第三方保健品
      },
      {
        path: '/prescriptionApi.html',
        name: 'prescriptionApi',
        component: () => import('../views/medicineapi/PrescriptionApi')// 第三方方剂
      },
      {
        path: '/searchApi.html',
        name: 'searchApi',
        component: () => import('../views/medicineapi/SearchApi')// 第三搜索
      },
      // {
      //   path: '/proprietaryChineseMedicinesDeatils/:id.html',
      //   name: 'proprietaryChineseMedicinesDeatils',
      //   component: () => import('../views/medicinecagetory/particulars/ProprietaryChineseMedicinesDeatils')// 西药
      // },
      // {
      //   path: '/traditionalChineseMedicineDetails/:id.html',
      //   name: 'traditionalChineseMedicineDetails',
      //   component: () => import('../views/medicinecagetory/particulars/TraditionalChineseMedicineDetails')// 西药
      // },
      // {
      //   path: '/westernMedicineThemeDetails/:id.html',
      //   name: 'westernMedicineThemeDetails',
      //   component: () => import('../views/medicinecagetory/particulars/WesternMedicineThemeDetails')// 西药
      // },
      {
        path: '/chineseBodyDiscernment/:id.html',
        name: 'chineseBodyDiscernment',
        component: () => import('../views/medicinecagetory/particulars/ChineseBodyDiscernment')
      },
      {
        path: '/bodyDiscernmentInfo.html',
        name: 'bodyDiscernmentInfo',
        component: () => import('../views/medicinecagetory/BodyDiscernmentInfo')
      }
    ]
  },
  {
    path: '/login/index.html',
    name: 'login',
    component: () => import('../views/login/LoginView.vue')
  },
  {
    path: '/proprietaryChineseMedicinesDeatils/:id.html',
    name: 'proprietaryChineseMedicinesDeatils',
    component: () => import('../views/medicinecagetory/particulars/ProprietaryChineseMedicinesDeatils')// 西药
  },
  {
    path: '/traditionalChineseMedicineDetails/:id.html',
    name: 'traditionalChineseMedicineDetails',
    component: () => import('../views/medicinecagetory/particulars/TraditionalChineseMedicineDetails')// 西药
  },
  {
    path: '/westernMedicineThemeDetails/:id.html',
    name: 'westernMedicineThemeDetails',
    component: () => import('../views/medicinecagetory/particulars/WesternMedicineThemeDetails')// 西药
  },
  {
    path: '/backend.html', // backend.html
    name: 'backend',
    component: backend,
    redirect: 'index.html', // 进入主页面显示首页
    children: [
      {
        path: '/index.html',
        name: 'dateShowIndex',
        component: () => import('../components/DateShow')
      },
      {
        path: '/demo01',
        name: 'demo01',
        component: () => import('../views/log/Demo01Test.vue')
      },
      {
        path: '/oplog',
        name: 'oplog',
        component: () => import('../views/log/OperationLog.vue')
      },
      {
        path: '/demo02',
        name: 'demo02',
        component: () => import('../views/Demo01Test')
      },
      {
        path: '/demo03',
        name: 'demo03',
        component: () => import('../views/user/UserView')
      },
      {
        path: '/menu',
        name: 'menu',
        component: () => import('../views/user/MenuView')
      },
      {
        path: '/demo04',
        name: 'demo04',
        component: () => import('../views/health/HealthAssessment')
      },
      {
        path: '/demo05',
        name: 'demo05',
        component: () => import('../views/classmedicine/WesternMedicine')
      },
      {
        path: '/demo06',
        name: 'demo06',
        component: () => import('../views/classmedicine/TranditionalMedicine')
      },
      {
        path: '/chinesePatentDrugMedincine',
        name: 'chinesePatentDrugMedincine',
        component: () => import('../views/classmedicine/ChinesePatentDrugMedincine')
      },
      {
        path: '/demo07',
        name: 'demo07',
        component: () => import('../views/cagetory/DrugClassification')
      },
      {
        path: '/demo08',
        name: 'demo08',
        component: () => import('../views/personal/MyInfo')
      },
      {
        path: '/demo09',
        name: 'demo09',
        component: () => import('../views/user/AdminView')
      }
    ]
  }
]

const router = new VueRouter({
  routes,
  mode: 'hash'
})
// // 导航守卫
// 导航守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  const isBackendPage = to.matched.some(record => record.path.startsWith('/backend.html'))
  // const isFrontPage = to.matched.some(record => record.path.startsWith('/'))
  // if (isFrontPage) {
  //   const token = localStorage.getItem('token') || localStorage.getItem('ordinaryUserToken')
  //   if (!token) {
  //     next({
  //       path: '/webPage.html',
  //       query: { redirect: to.fullPath }
  //     })
  //   } else {
  //     next()
  //   }
  // } else {
  //   // 不是后台页面，直接放行
  //   next()
  // }
  if (isBackendPage) {
    const token = localStorage.getItem('token')
    if (!token) {
      next({
        path: '/login/index.html',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    // 不是后台页面，直接放行
    next()
  }
})

// 定义路由导航后置守卫
router.afterEach(() => {
  NProgress.done()
})
export default router
